package com.bjsxt.back.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.action.BaseAction;
import com.bjsxt.common.util.ContantFinalUtil;
import com.bjsxt.common.util.Encrypt;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.system.pojo.AInverstor;
import com.bjsxt.system.pojo.Region;
import com.bjsxt.system.service.IAreaService;

@Controller
@RequestMapping("back")
public class InverstorBackAction extends BaseAction 
{	
	@Resource
	private IAreaService areaService;
	@Resource
	private Encrypt encrypt;
	/**
	 * 展示出资放列表
	 * @param req
	 * @return
	 */
	@RequestMapping("/inverstorList.htm")
	public String inverstorList(HttpServletRequest req)
	{
		HashMap condMap = new HashMap();
		/* 接收参数 */
		String keyword = req.getParameter("keyword");
		String status = req.getParameter("status");
		String endTime = req.getParameter("endTime");
		String startTime = req.getParameter("startTime");
		if(keyword != null)
		{
			condMap.put("keyword", keyword);
		}
		if(status != null)
		{
			condMap.put("status", status);
		}
		if(endTime != null && !"".equals(endTime))
		{
			/* 将时间字符串转换成日期 */
			Date endDate = this.dateFormatUtil.format(endTime, "yyyy-MM-dd HH:mm:ss");
			condMap.put("endTime", endDate);
		}
		if(startTime != null && !"".equals(startTime))
		{
			/* 将时间字符串转换成日期 */
			Date startDate = this.dateFormatUtil.format(startTime, "yyyy-MM-dd HH:mm:ss");
			condMap.put("startTime", startDate);
		}
		PageInfoUtil pageInfoUtil = this.getPage(req);
		List<AInverstor> inverstorList = areaService.findListAInverstor(pageInfoUtil, condMap);
		req.setAttribute("inverstorList", inverstorList);
		req.setAttribute("status", status);
		req.setAttribute("keyword", keyword);
		req.setAttribute("endTime", endTime);
		req.setAttribute("startTime", startTime);
		req.setAttribute("pageInfoUtil", pageInfoUtil);
		return "back/inverstor/inverstorList";
	}
	
	@RequestMapping(value="/updateSubmitInverstor.htm",produces="text/html;charset=UTF-8")
	public @ResponseBody String undateSubmitInverstor(HttpServletRequest req,MultipartFile file)
	{
		JSONObject json=null;
		HashMap condMap = new HashMap();
		String code = req.getParameter("code");
		System.out.println("---------------"+file);
		/* 将需要更新的对象查出来 需要更新的更新 */
		condMap.put("code", code);
		AInverstor inverstor = areaService.findOneAInverstor(condMap);
		/* 更新属性 */
		inverstor.setName(req.getParameter("name"));
		inverstor.setCode(code);
		inverstor.setUrl(req.getParameter("url"));
		inverstor.setContactName(req.getParameter("contactName"));
		inverstor.setContactPhone(req.getParameter("contactPhone"));
		inverstor.setTotalNum(Integer.valueOf(req.getParameter("totalNum")));
		inverstor.setPubTime(this.dateFormatUtil.format(req.getParameter("pubTime"), ContantFinalUtil.BUNDLE.getString("pattern")));
		inverstor.setCoopType(Integer.valueOf(req.getParameter("coopType")));
		inverstor.setStatus(Byte.valueOf(req.getParameter("status")));
		String content = req.getParameter("content");
		content=encrypt.encryptPassword(content);
		inverstor.setContent(content);
		inverstor.setUpdateTime(new Date());
		/* 得到文件真实公共路径 */
		String pubPath = req.getSession().getServletContext().getRealPath("/");
		System.out.println("------------realPath:"+pubPath);
		condMap.put("pubPath", pubPath);
		condMap.put("file", file);
		try
		{
			json = areaService.updateAInverstor(inverstor, condMap);
			this.info=json.getString("info");
		} catch (IOException e)
		{
			this.info="更新失败";
			e.printStackTrace();
		}
		json = this.returnJuiJSON(req, this.info);
		return json.toJSONString();
	} 
	
	/**
	 * 更新投资方信息
	 * @return
	 */
	@RequestMapping("/updateInverstor.htm")
	public String updateInverstor(HttpServletRequest req)
	{
		/* 先把投资方给查出来 信息在街面上显示 */
		String iid = req.getParameter("iid");
		HashMap condMap = new HashMap();
		condMap.put("id", iid);
		/* 将出资方放到作用域里 */
		AInverstor inverstor = areaService.findOneAInverstor(condMap);
		req.setAttribute("investor", inverstor);
		System.out.println(inverstor);
		System.out.println("--------------updateInverstor------------");
		return "back/inverstor/updateInverstor";
	}
}
